package com.mt.skyhoist.util.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mt.skyhoist.R;

public class ModifyNameDialog extends Dialog {
    public ModifyNameDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_name_modify);
        setCanceledOnTouchOutside(false);
        setCancelable(false);

        initView();
    }

    private TextView msgTv;
    private EditText nameEv;
    private Button cancelBtn;
    private Button confirmBtn;

    private void initView() {
        msgTv = findViewById(R.id.msg_tv);
        nameEv = findViewById(R.id.name_ev);
        cancelBtn = findViewById(R.id.cancel_btn);
        confirmBtn = findViewById(R.id.ok_btn);

        cancelBtn.setOnClickListener(onClickListener);
        confirmBtn.setOnClickListener(onClickListener);

        msgTv.setText(msg);

        nameEv.setText(name);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == cancelBtn) {
                if (listener != null) {
                    listener.OnCancel();
                }
                dismiss();
                return;
            }
            if (v == confirmBtn) {
                if (listener != null) {
                    listener.OnConfirm(nameEv.getText().toString());
                }
                dismiss();
                return;
            }
        }
    };


    private OnBtnClickListener listener;

    public void setListener(OnBtnClickListener listener) {
        this.listener = listener;
    }

    private String msg;

    public void setMessage(String msg) {
        this.msg = msg;
        if (msgTv != null) {
            msgTv.setText(msg);
        }
    }

    private String name;
    public void setName(String name) {
        this.name = name;
        if(nameEv == null) {
            return;
        }
        nameEv.setText(name);
    }

    public interface OnBtnClickListener {
        void OnConfirm(String newName);
        void OnCancel();
    }
}
